package com.hg.library.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.Button;

/**
 * Created by admin on 2017/2/14.
 */

public class InputButton extends Button {
    private static final String TAG = "InputButton";

    private OnInputTextListener inputTextListener;

    public InputButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnInputTextListener(OnInputTextListener inputTextListener) {
        this.inputTextListener = inputTextListener;
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return new CustomCon(this, false);
    }

    class CustomCon extends BaseInputConnection {

        public CustomCon(View targetView, boolean fullEditor) {
            super(targetView, fullEditor);
        }

        @Override
        public boolean commitText(CharSequence text, int newCursorPosition) {
            if (inputTextListener != null) {
                inputTextListener.onInputText(InputButton.this, text);
            }
            return super.commitText(text, newCursorPosition);
        }
    }

    public interface OnInputTextListener {
        void onInputText(View view, CharSequence charSequence);
    }
}
